package ctrl

import (
	"fmt"
	"iot-base/common/cache"
	"iot-base/common/notify"
)

type CtrlRouter struct {
	notify.CTRLOR
}

func init() {
	fmt.Println("------ctrl----------init--------")
	notify.SetCtrlRouter(&CtrlRouter{})

}

//GetCtrlTopic 获取ctrl的topic
func (router *CtrlRouter) GetCtrlTopic(tid string, serviceID string, protocolPath string) (string, string, bool) {
	if tid == "ALL" && protocolPath != "" {
		return protocolPath + ".ctrl", "", true
	}
	pInfo, _ := cache.GetThing(tid)
	if pInfo != nil {
		for {
			if pInfo == nil {
				break
			}
			if pInfo.Thing.ProtocolPath != "" {
				return pInfo.Thing.ProtocolPath + ".ctrl", "", true
			}
			if pInfo.Thing.ModelID == "IOTBASEmEdgeCTL" {
				return "IOTBASEmEdgeCTL.ctrl", pInfo.Thing.UID, true
			}
			if pInfo.Thing.ProxyThingID != "" {
				pInfo, _ = cache.GetThing(pInfo.Thing.ProxyThingID)
			} else {
				break
			}
		}

	}
	return "", "", false
}
